import java.util.ArrayList;
import java.util.List;

import css.sword.util.HttpClientService;
import org.apache.http.HttpResponse;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.message.BasicNameValuePair;

/**
 * http client 使用
 * */
public class HttClientUseDemo extends HttpClientService {

    public static void main(String[] args) {

        new HttClientUseDemo().getConfCall();
    }

    public void getConfCall() {

        String url = "http://220.181.14.110/xxxxx/xxxxx/searchbyappid.do";

        List<BasicNameValuePair> urlParams = new ArrayList<BasicNameValuePair>();
        urlParams.add(new BasicNameValuePair("appid", "2"));
        exeHttpReq(url, false, urlParams, null, new GetConfCall());
    }

    public void exeHttpReq(String baseUrl, boolean isPost,
                           List<BasicNameValuePair> urlParams,
                           List<BasicNameValuePair> postBody,
                           FutureCallback<HttpResponse> callback) {

        try {
            System.out.println("enter exeAsyncReq");
            exeAsyncReq(baseUrl, isPost, urlParams, postBody, callback);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 被回调的对象，给异步的httpclient使用
     *
     * */
    class GetConfCall implements FutureCallback<HttpResponse> {

        /**
         * 请求完成后调用该函数
         */
        @Override
        public void completed(HttpResponse response) {

            System.out.println(response.getStatusLine().getStatusCode());
            System.out.println(getHttpContent(response));

            HttpClientUtils.closeQuietly(response);

        }

        /**
         * 请求取消后调用该函数
         */
        @Override
        public void cancelled() {

        }

        /**
         * 请求失败后调用该函数
         */
        @Override
        public void failed(Exception e) {
//            log.info("======发送async http post请求失败======", e);
//
//            // 记录到数据库
//            if (null != value) {
//                // 重试3次
//                if (retryNo < AsyncHttpProxy.maxRetryTimes) {
//                    AsyncHttpProxy.getAsyncHttpProxy().httpPost(operation, url, index, id, value, ++retryNo);
//                } else {
//                    new SearchErrorHanlder().saveError(operation, index, id, value, e);
//                }
//            }
        }

    }
} 